package com.zyt.springboot.sm2Encrypt;

import sviolet.smcrypto.SmCryptoUtil;

/**
 * @Description:RSA解密工具类
 * @date 2019年12月19日下午2:56:08
 */
public class Decrypt {
    public static void main(String[] args) throws Exception {
        // 网关返回的密文
        String secret =
                "0406f22b62cf4fe9bb5655e9c0eb0abeed1fda59c85922fe1061962e26bda4ad57ffe2588b966a2bc3c86a9a1a26f4fff244a05e57046eb0a6134c3f197aaf49dc65d31990eaa27bda81aaeedd30ca8b0e9d8378c864df8151c598dc3c50ea16991b9cc31b3a5cc15a3557c1302b";
        // 私钥（在平台接口详情处复制）
//        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIEVHehrvKJ9jSvVFbwuwT1WaYGWamOzCJ9DLby/5PxaPbjcGyJtXu+O3+S3/u/dbmJSvVjfxbUyRrcPNK6cfqAI1ygUn8fp/S+dmELklEwC932R/8kluKfLEyAXimmiZA9W4mkRYlYNhPjHH1MvgG2O1Lvcx5WRLQlVa7bHgOJjAgMBAAECgYAb5MZLvUxMCLmC6U8DFU9ow82U1T40QTHxyICPsx/2cEG6OsYaUGwddFBpimEq0DzUPnDwTZrZpv2k/UIzIVozRqgI25eBQ17dNQoVI1N2d+c6dSZwgm/lRQ5ZQ4oXRnuxxg5eQo4/uvhyBuMw+QCEb1NyeqyX0eFTGVrFQWIg4QJBAOBDbuUtm0vI2ZQmZo5n1plYPHAQyuWEZ515B6R65qKksqTiGRf2uuONiDNOE8gMyg4QPAIJc0XAMNM0n4OAnhsCQQCTWX1SlIpY3Er5zaZxbBd/ko7kGHeHT1WsCUKto+fIj3RhThJsz9E6FnDqM4OoWxCUZp0HHouZmoaJjn9+AHFZAkEAmx3JTHdZHPgVlqVg9krYYkRnuVPEIzCLwp3+LURc1k54yhchKDQeXFp+xI3SnOJLt6J+/yGuO6aNnuidFIYX1QJALhRrU7ruNPHXZJ2Ccq/5/URI+V4Ja0BNK3rN5l1+q8LpA1EWA/9NkBvaffNUNf8chTw92S8dOxEPB8+PYUkkKQJBAJatZtHyRFdxEMzOd+fRS3ikjFsOA3DkkmruDUQ0YL/JxAtXj6swRHqAMhhoNJLlYx9FfCgpIJApRZPzVX8eVUs=";
        String sm2PrivateKey = "00b82396992e8e5e739939df8f1b44fbb93231028490d4ffd2ec37ddabdf98801c";
        //公钥
//        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBFR3oa7yifY0r1RW8LsE9VmmBlmpjswifQy28v+T8Wj243BsibV7vjt/kt/7v3W5iUr1Y38W1Mka3DzSunH6gCNcoFJ/H6f0vnZhC5JRMAvd9kf/JJbinyxMgF4ppomQPVuJpEWJWDYT4xx9TL4BtjtS73MeVkS0JVWu2x4DiYwIDAQAB";
        String sm2PublicKey = "0461496c52c2ca3b166b443a1ee59a1848047d8354bc9f6550e04e2c0138ee0326ad172394061e4063204898176d3c3aaf5091765c9234461af8b307e3194070c1";
        String content = getContent(secret, sm2PrivateKey);
        System.out.println("解密数据：" + content);
        String str = "{\"iid\":\"111\"}";
        System.out.println("加密数据：" + SmCryptoUtil.sm2Encode(str, sm2PublicKey));
//        System.out.println("加密数据：" + SignatureUtil.encrypt(str, SignatureUtil.getPublicKey(publicKey)));
    }

    /**
     * @param privateKey
     * @param secret
     * @return
     * @throws Exception
     * @Description:解密网关返回的密文
     * @date 2019年12月19日下午1:50:00
     */
    public static String getContent(String secret, String privateKey) throws Exception {
        String decrypt = "";
        // 如果应用加密方式选择的是RSA
//         decrypt = SignatureUtil.decrypt(secret, SignatureUtil.getPrivateKey(privateKey));
        // 如果加密方式选择的是国密
        decrypt = SmCryptoUtil.sm2Decode(secret, privateKey);
        return decrypt;
    }
}
